package com.fcscs.cservice.client.main.configuration.restaurant;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.BaseFormDialog;
import com.fcscs.core.gwt.common.ui.CenteredInfo;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.controller.BookingServiceController;
import com.fcscs.cservice.domain.PropertyConfigRestaurant;
import com.fcscs.cservice.domain.Restaurant;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class RestAreaCommonConfig extends BaseFormDialog<Restaurant> implements
		FormSubmitListener<Restaurant> {
	private static RestAreaCommonConfig INSTANCE;
	private MyFormPanel myFormPanel;
	private VerticalPanel vp;
	private FormButton saveBtn;
	private FormButton cancelBtn;
	private RestaurantCommonConfigArea commonConfigArea;

	public static RestAreaCommonConfig getInstance(PropertyConfigRestaurant result) {
		if (null == INSTANCE) {
			INSTANCE = new RestAreaCommonConfig(result);
		}
		INSTANCE.setHeading(CServiceConstants.CSERVICE_CONSTANTS.restaurantConf());
		return INSTANCE;
	}

	public RestAreaCommonConfig(PropertyConfigRestaurant entity) {
		if (null == INSTANCE) {
			myFormPanel = new MyFormPanel(this, entity);
		}
		add(myFormPanel);
		setSize(750, 760);
	}

	public class MyFormPanel extends FcsFormPanel<Restaurant> {

		protected MyFormPanel(FormSubmitListener<Restaurant> l, PropertyConfigRestaurant entity) {
			super(l);

			setHeaderVisible(false);

			if (null == vp) {
				vp = new VerticalPanel();

				commonConfigArea = new RestaurantCommonConfigArea();
				vp.add(commonConfigArea);

				saveBtn = new FormButton(CoreConstants.CORE_CONSTANTS.save());
				saveBtn.setId("RestaurantConfigPanel_saveBtn");
				saveBtn.setWidth(60);
				saveBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						if (null == commonConfigArea.reqPopupArea.getValue()
								|| null == commonConfigArea.bookingTitleField.getValue()
								|| null == commonConfigArea.bookingComfMessArea.getValue()
								|| null == commonConfigArea.timeoutTitleField.getValue()
								|| null == commonConfigArea.timeoutAlertMessArea.getValue()
								|| null == commonConfigArea.bookingReqTitleField.getValue()
								|| null == commonConfigArea.bookingMessTempArea.getValue()) {
							return;
						} else {
							PropertyConfigRestaurant propConfigRestaurant = new PropertyConfigRestaurant();
							propConfigRestaurant.setReservationReqAckPopup(commonConfigArea.reqPopupArea
									.getValue());
							propConfigRestaurant.setBookingConfirmTitle(commonConfigArea.bookingTitleField
									.getValue());
							propConfigRestaurant.setBookingConfirmBody(commonConfigArea.bookingComfMessArea
									.getValue());
							propConfigRestaurant.setRestaurantBkTempTitle(commonConfigArea.timeoutTitleField
									.getValue());
							propConfigRestaurant
									.setRestaurantBkTempBody(commonConfigArea.timeoutAlertMessArea.getValue());
							propConfigRestaurant
									.setBookingReqMsgTemplateTitle(commonConfigArea.bookingReqTitleField
											.getValue());
							propConfigRestaurant
									.setBookingReqMsgTemplate(commonConfigArea.bookingMessTempArea.getValue());
							savePropertyConfigRestaurant(propConfigRestaurant);
						}
					}
				});
				getButtonBar().add(saveBtn);

				cancelBtn = new FormButton(CoreConstants.CORE_CONSTANTS.cancel());
				cancelBtn.setId("RestaurantConfigPanel_cancelBtn");
				cancelBtn.setWidth(60);
				cancelBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						RestAreaCommonConfig.this.hide();
					}
				});
				getButtonBar().add(cancelBtn);
				add(vp);

				updateConfig(entity);  
			}
		}

		@Override
		public void updateForm(Restaurant entity) {
			// TODO Auto-generated method stub

		}

		@Override
		public void populateEntity(Restaurant entity) {
			// TODO Auto-generated method stub

		}

		@Override
		public void setFocus() {
			// TODO Auto-generated method stub

		}

	}

	public void savePropertyConfigRestaurant(final PropertyConfigRestaurant propConfigRest) {
		BookingServiceController.getInstance().savePropertyConfigRestaurant(propConfigRest,
				new AsyncCallbackListener<PropertyConfigRestaurant>() {

					@Override
					public void onLogin(String username) {
						BookingServiceController.getInstance().savePropertyConfigRestaurant(propConfigRest,this);
					}

					@Override
					public void onSuccess(PropertyConfigRestaurant result) {
						if(null != result){
							INSTANCE.hide();
							CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.saved());
						}
					}

					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(
								CoreConstants.CORE_CONSTANTS.system(),
								ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
								CoreConstants.CORE_CONSTANTS.systemError(), null);
					}
				});
	}

	public void updateConfig(PropertyConfigRestaurant entity) {
		commonConfigArea.reqPopupArea.setValue(entity.getReservationReqAckPopup());
		commonConfigArea.bookingTitleField.setValue(entity.getBookingConfirmTitle());
		commonConfigArea.bookingComfMessArea.setValue(entity.getBookingConfirmBody());
		commonConfigArea.timeoutTitleField.setValue(entity.getRestaurantBkTempTitle());
		commonConfigArea.timeoutAlertMessArea.setValue(entity.getRestaurantBkTempBody());
		commonConfigArea.bookingReqTitleField.setValue(entity.getBookingReqMsgTemplateTitle());
		commonConfigArea.bookingMessTempArea.setValue(entity.getBookingReqMsgTemplate());
	}

	@Override
	protected FcsFormPanel<Restaurant> getFormPanel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void formSubmitCallBack(FcsFormPanel<Restaurant> form, SubmitType submitType) {
		// TODO Auto-generated method stub

	}

}
